﻿<%@ WebHandler Language="C#" Class="file" %>

using System;
using System.Web;
using System.Data;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Text;
using common;
using common.web;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Drawing;
using System.Drawing.Imaging;

public class file : IHttpHandler {

    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "application/json";
        context.Response.AddHeader("Access-Control-Allow-Origin", "*");
        string _t = context.Request["t"];
        context.Response.Write(getData(_t));
    }

    protected string getData(string t)
    {
        switch (t)
        {
            case "GetFileList":
                return GetFileList();               //文件列表
            case "ViewFile":
                return ViewFile();                  //查看文件
            case "EditFile":
                return EditFile();                  //修改文件
            case "FileNoSale":
                return FileNoSale();                //文件下架
        }
        return "缺少参数!";
    }

    /// <summary>
    /// 文件列表
    /// </summary>
    protected string GetFileList()
    {
        JObject jo = new JObject();
        JArray ja = new JArray();
        string _uid = getUid();
        if(_uid == "0")
        {
            jo.Add("Result", 1);
            jo.Add("Message", "uid参数错误");
            return JsonConvert.SerializeObject(jo);
        }
        int _page = pgfunc.getPage(HttpContext.Current.Request["page"]);
        int _pagesize = pgfunc.getPage(HttpContext.Current.Request["pagesize"]);
        int rsct, pgct;
        string _orderby = "id";
        string _sort = "desc";
        string strWhere = "";
        string _deptid = func.safeStr(Req("deptid"));
        string _typeid = func.safeStr(Req("typeid"));
        if(!string.IsNullOrEmpty(_deptid) && func.chkRegex(_deptid, @"^\d{1,9}(\.\d{1,2})?$") == false)
        {
            jo.Add("Result", 2);
            jo.Add("Message", "deptid参数错误");
            return JsonConvert.SerializeObject(jo);
        }
        if(!string.IsNullOrEmpty(_typeid) && func.chkRegex(_typeid, @"^\d{1,9}(\.\d{1,2})?$") == false)
        {
            jo.Add("Result", 2);
            jo.Add("Message", "typeid参数错误");
            return JsonConvert.SerializeObject(jo);
        }
        string _keyword = func.safeStr(Req("keyword"));
        if (!string.IsNullOrEmpty(Req("orderby"))) _orderby = func.safeStr(Req("orderby"));
        if (!string.IsNullOrEmpty(Req("sort"))) _sort = func.safeStr(Req("sort"));
        if (!string.IsNullOrEmpty(_deptid)) strWhere = strWhere + " and fDptid = " + _deptid;
        if (!string.IsNullOrEmpty(_typeid)) strWhere = strWhere + " and fFileclid = " + _typeid;

        string sql = pgfunc.GetSqlstr5(_page, _pagesize, "id,cFilename,fFiletype,tDate,cPic,cDescribe","[t_Filecollection] with(nolock) where cFilename like '%" + _keyword + "%'" + strWhere, "order by  " + _orderby + " " + _sort , out rsct, out pgct);
        using (IDataReader dr = db.dtReader(sql))
        {
            while (dr.Read())
            {
                dynamic inv = new JObject();
                inv.id = dr["id"].ToString();
                inv.Filename = dr["cFilename"].ToString();
                inv.Date = dr["tDate"].ToString();
                inv.Pic = dr["cPic"].ToString();
                inv.Describe = dr["cDescribe"].ToString();
                ja.Add(inv);
            }
        }
        jo.Add("Rsct", rsct);
        jo.Add("Pgct", pgct);
        jo.Add("List", ja);
        jo.Add("Result", 0);
        jo.Add("Message", "");
        return JsonConvert.SerializeObject(jo);
    }

    /// <summary>
    /// 文件查看
    /// </summary>
    protected string ViewFile()
    {
        JObject jo = new JObject();
        JArray ja = new JArray();
        string _uid = getUid();
        if(_uid == "0")
        {
            jo.Add("Result", 1);
            jo.Add("Message", "uid参数错误");
            return JsonConvert.SerializeObject(jo);
        }
        string _id = func.safeStr(Req("id"));
        if(string.IsNullOrEmpty(_id) || func.chkRegex(_id, @"^\d{1,9}(\.\d{1,2})?$") == false)
        {
            jo.Add("Result", 2);
            jo.Add("Message", "参数错误");
            return JsonConvert.SerializeObject(jo);
        }

        string sql = "select id,cFilename,fFiletype,tDate,cPic,cDescribe from [t_Filecollection] with(nolock) where id = " + _id;
        using (IDataReader dr = db.dtReader(sql))
        {
            while (dr.Read())
            {
                dynamic inv = new JObject();
                inv.id = dr["id"].ToString();
                inv.Filename = dr["cFilename"].ToString();
                inv.Date = dr["tDate"].ToString();
                inv.Pic = dr["cPic"].ToString();
                inv.Describe = dr["cDescribe"].ToString();
                ja.Add(inv);
            }
        }
        jo.Add("List", ja);
        jo.Add("Result", 0);
        jo.Add("Message", "");
        return JsonConvert.SerializeObject(jo);
    }

    /// <summary>
    /// 文件修改
    /// </summary>
    protected string EditFile()
    {
        JObject jo = new JObject();
        JArray ja = new JArray();
        string _uid = getUid();
        if(_uid == "0")
        {
            jo.Add("Result", 1);
            jo.Add("Message", "uid参数错误");
            return JsonConvert.SerializeObject(jo);
        }
        string _id = func.safeStr(Req("id"));
        string _filename = func.safeStr(Req("filename"));
        string _deptid = func.safeStr(Req("deptid"));
        string _filetype = func.safeStr(Req("filetype"));
        string _ck = func.safeStr(Req("ck"));
        string _des = func.safeStr(Req("des"));
        if(string.IsNullOrEmpty(_id) || func.chkRegex(_id, @"^\d{1,9}(\.\d{1,2})?$") == false)
        {
            jo.Add("Result", 2);
            jo.Add("Message", "参数错误");
            return JsonConvert.SerializeObject(jo);
        }
        if(string.IsNullOrEmpty(_filename))
        {
            jo.Add("Result", 2);
            jo.Add("Message", "参数错误");
            return JsonConvert.SerializeObject(jo);
        }
        if(string.IsNullOrEmpty(_filetype) || func.chkRegex(_filetype, @"^\d{1,9}(\.\d{1,2})?$") == false)
        {
            jo.Add("Result", 2);
            jo.Add("Message", "参数错误");
            return JsonConvert.SerializeObject(jo);
        }
        if(string.IsNullOrEmpty(_ck) || func.chkRegex(_ck, @"^\d{1,9}(\.\d{1,2})?$") == false)
        {
            jo.Add("Result", 2);
            jo.Add("Message", "参数错误");
            return JsonConvert.SerializeObject(jo);
        }
        db.ExecNQ("update [t_Filecollection] set cFilename = '" + _filename + "', fDptid = " + _deptid + ", fFiletype = " + _filetype + ", cCk = '" + _ck + "',cDescribe= '" + _des + "' where id = " + _id);
        jo.Add("Result", 0);
        jo.Add("Message", "");
        return JsonConvert.SerializeObject(jo);
    }

    /// <summary>
    /// 文件下架
    /// </summary>
    protected string FileNoSale()
    {
        JObject jo = new JObject();
        JArray ja = new JArray();
        string _uid = getUid();
        if(_uid == "0")
        {
            jo.Add("Result", 1);
            jo.Add("Message", "uid参数错误");
            return JsonConvert.SerializeObject(jo);
        }
        string _id = func.safeStr(Req("id"));
        string _offsale = func.safeStr(Req("offsale"));
        if(string.IsNullOrEmpty(_id) || func.chkRegex(_id, @"^\d{1,9}(\.\d{1,2})?$") == false)
        {
            jo.Add("Result", 2);
            jo.Add("Message", "参数错误");
            return JsonConvert.SerializeObject(jo);
        }
        db.ExecNQ("update t_Filecollection set fIsonsale = 0, cOffsale = '" + _offsale + "' where id = " + _id);
        jo.Add("Result", 0);
        jo.Add("Message", "");
        return JsonConvert.SerializeObject(jo);
    }

    protected string getUid()
    {
        string _uid = getID(HttpUtility.UrlDecode(func.NoNull(Req("uid")).Replace("+", "%2b")));
        if (_uid == "0")
            _uid = pg.usid();
        return _uid;
    }

    /// <summary>
    /// 解密id
    /// </summary>
    protected string getID(string OpenID)
    {
        int id = 0;
        if (!string.IsNullOrEmpty(OpenID) && int.TryParse(new KeyPwd().Decrypto(OpenID), out id))
            return id.ToString();
        else
            return "0";
    }

    protected string Req(string s)
    {
        return HttpContext.Current.Request[s];
    }

    public bool IsReusable {
        get {
            return false;
        }
    }

}